<?php
include_once (MODEL.'/Repository.php');
include_once (MODEL . '/WantModel.php');

class WantRepository extends Repository
{
    public $collection;
    
	public function __construct()
	{
    	parent::__construct();
        $this->collection=$this->DB->selectCollection('Want');
	}
	
	public function GetListCategory() 
	{
		$this->collection = $this->DB->selectCollection('Category');
		return $this->collection->find();
	}
	
	public function GetListRequest()
	{
		$list= $this->DB->selectCollection('Want')->find()->limit(6);
		$listRequest  =  array();
		while($list->hasNext())
		{
			$want = new WantModel();
			$result = $list->getNext();
			$want->ID  = $result['_id'];
			$want->Image = $result['Image'];
			$want->Name = $result['Name'];
			$want->Description = $result['Description'];
			$listRequest[] = $want;
		}
		return $listRequest;
	}
	
    
    public function TotalByCategory($categoryID)
    {
       if ($categoryID !='') $filter = array('CategoryID'=>new MongoId($categoryID)); 
        else $filter = array();
       return $this->collection->find($filter)->count();
    }
    
    public function ViewListWantByCategory($categoryID, $start, $limit)
    {
        if ($categoryID !='') $filter = array('CategoryID'=>new MongoId($categoryID)); 
        else $filter = array();
        $cursor = $this->collection->find($filter)->skip($start)->limit($limit);
        $listWant = array();

		while($cursor->hasNext()) 
		{
			$want = new WantModel(); 
			$result = $cursor->getNext();
			$want->ID = $result['_id'];
			$want->Name = $result['Name'];
			$want->Location = $result['Location'];
			$want->Image = $result['Image'];
			
			$user = $this->DB->selectCollection('Account')->findOne(array('_id' => new MongoId($result['AccountID'])));
			$want->OwnerID = $user['_id'];
			$want->OwnerName = $user['FirstName'];
						
			$listWant[] = $want;
		}
		
		return $listWant;
    }
    
    public function CreateWant($model) 
    {
    	//$this->collection = $this->DB->selectCollection('Want');
    	                                               
       	$location = '';
        $location .= $model->Street;
        $location .= ', ' . $model->City;
        $location .= ', ' . $model->Province;
        
        $data = array(
            'Name'  => $model->Name,
            'AccountID' => $model->AccountID,
            'Location'  => $location,
            'CreateDate'  => new MongoDate(),
            'Image'  => $model->Image['name'],
            'ActiveDate' => '',
            'Description'  => $model->Description,
       );                           
       
       $this->collection->insert($data);       
       return $data['_id'];                                                                
    }
     public function DeleteWant($filter)
    {
        $this->collection->remove($filter);
    }
    public function GetDetail($wantID)
    {
        include_once (MODEL . '/WantModel.php');
        
        $filter = array('_id'=> new MongoId($wantID));
        $result = $this->collection->findOne($filter);
        $want = new WantModel();
		$want->ID = $result['_id'];
		$want->Name = $result['Name'];
        $Category = $this->DB->selectCollection('Category')->findOne(array('_id' => new MongoId($result['CategoryID'])));
        $want->Category = $Category['Name'];
		$want->Location = $result['Location'];
		$want->Image = $result['Image'];
		$user = $this->DB->selectCollection('Account')->findOne(array('_id' => new MongoId($result['AccountID'])));
		$want->OwnerID = $user['_id'];
		$want->OwnerName = $user['FirstName'];
        $want->Description = $result['Description'];
        return $want;
    }
   
    public function EditWant($wantID,$wantName,$wantCategory,$wantDescription,$wantLocation)
    {
    	$image = $wantID.".jpg";
    	$this->collection->update(array('_id'=>new MongoId($wantID)),array('$set'=>array('Name'=>$wantName,'Location'=>$wantLocation,'CategoryID'=>new mongoID($wantCategory),'Description'=>$wantDescription,'Image'=>$image)),array("upsert"=>false));
    	 
    }
}